Azureの Web App + Databaseテンプレートで作成されたMySQLへ接続してみた

Azureの Web App + Databaseテンプレートで作成されたMySQLへ接続してみた

Clock Icon2022.03.30

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

いわさです。

Azure App Service + Azure Databaseの公式テンプレートがMarketplaceにありまして、先日そちらを使って環境を構築していました。

しかし、構築されたAzure Database for MySQLにローカルクライアントからアクセスしようとしてもアクセスが出来ませんでした。

$ mysql -u mdghqxvlkp -h hogeiwasa-server.mysql.database.azure.com -p sys
Enter password: 
ERROR 2005 (HY000): Unknown MySQL server host 'hogeiwasa-server.mysql.database.azure.com' (8)

VNET & プライベートDNS 統合されている

どうやら、このテンプレートでは各リソースがVNET統合されており、パブリックアクセスが許可されていないセキュアな状態でした。

このテンプレートで作成されるAzure Database for MySQL はFlexible Serverで、そしてVNETで統合が使われている状態でした。
以下のドキュメントに記述のあるとおり、VNETに対してプライベート接続が必要です。

  • 同じあるいはピアリングされたVNETから接続する
  • VPN/ExpressRouteを使用して接続する

  • Connect from Azure resources in the same virtual network or peered virtual network to your flexible server
  • Use VPN or ExpressRoute to connect from non-Azure resources to your flexible server
  • No public endpoint

Azure Bastionと仮想マシンを使って同一VNETから接続する

今回は手軽に同一VNET内に踏み台を用意してそこからアクセスしてみます。
VNETでAzure Bastionを有効化し仮想マシンを追加します。

まずは、Azure Databaseに統合されているVNETでBastionをデプロイします。
Azure BastionからVNETを指定する形でも、VNETのBastionメニューからデプロイボタンを押す形でも実行出来ます。

Bastion経由なのでプライベートな仮想マシンを作成します。
MySQLのクライアントとして利用するだけなのでイメージは何でも良いですが今回はUbuntu Server 20.04 LTS - Gen2を使いました。

接続する

デプロイされた仮想マシンの接続メニューからBastionを選択します。

Ubuntu ServerへMySQLクライアントをインストールします。

$ sudo apt-get -y install mysql-client
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  mysql-client-8.0 mysql-client-core-8.0 mysql-common
The following NEW packages will be installed:
  mysql-client mysql-client-8.0 mysql-client-core-8.0 mysql-common
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 4468 kB of archives.
After this operation, 67.1 MB of additional disk space will be used.
Get:1 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 mysql-client-core-8.0 amd64 8.0.28-0ubuntu0.20.04.3 [
4429 kB]
Get:2 http://azure.archive.ubuntu.com/ubuntu focal/main amd64 mysql-common all 5.8+1.0.5ubuntu2 [7496 B]
Get:3 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 mysql-client-8.0 amd64 8.0.28-0ubuntu0.20.04.3 [22.0 
kB]
Get:4 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 mysql-client all 8.0.28-0ubuntu0.20.04.3 [9420 B]
Fetched 4468 kB in 0s (12.2 MB/s) 
Selecting previously unselected package mysql-client-core-8.0.
(Reading database ... 57946 files and directories currently installed.)
Preparing to unpack .../mysql-client-core-8.0_8.0.28-0ubuntu0.20.04.3_amd64.deb ...
Unpacking mysql-client-core-8.0 (8.0.28-0ubuntu0.20.04.3) ...
Selecting previously unselected package mysql-common.
Preparing to unpack .../mysql-common_5.8+1.0.5ubuntu2_all.deb ...
Unpacking mysql-common (5.8+1.0.5ubuntu2) ...
Selecting previously unselected package mysql-client-8.0.
Preparing to unpack .../mysql-client-8.0_8.0.28-0ubuntu0.20.04.3_amd64.deb ...
Unpacking mysql-client-8.0 (8.0.28-0ubuntu0.20.04.3) ...
Selecting previously unselected package mysql-client.
Preparing to unpack .../mysql-client_8.0.28-0ubuntu0.20.04.3_all.deb ...
Unpacking mysql-client (8.0.28-0ubuntu0.20.04.3) ...
Setting up mysql-common (5.8+1.0.5ubuntu2) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Setting up mysql-client-core-8.0 (8.0.28-0ubuntu0.20.04.3) ...
Setting up mysql-client-8.0 (8.0.28-0ubuntu0.20.04.3) ...
Setting up mysql-client (8.0.28-0ubuntu0.20.04.3) ...
Processing triggers for man-db (2.9.1-1) ...

後は、Azure Databaseの概要に表示されているホストおよび、テンプレートデプロイ時に確認出来るユーザーとパスワードで接続してみましょう。

$ mysql -u mdghqxvlkp -h hogeiwasa-server.mysql.database.azure.com -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

無事、接続することができました。

さいごに

VNET統合されているのでプライベート接続する必要があったのですが、これまでの検証ではパブリックアクセスばかりだったので、Bastionを使って仮想マシンからアクセスしてみました。

Azure触ったばかりのころはどのサービスもパブリックアクセスすることが多かったと思いますが、本番環境でワークロードを構築するとなるとVNET統合するシーンも非常に多くなると思います。
今回はAzure Database for MySQLでしたが、他のVNET統合サービスでも同じような流れになると思います。

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.